//
//  ShopHeaderBannerView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/22.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "ShopHeaderBannerView.h"

#import "AdvertCoverView.h"

#import "MainPageHeaderModel.h"

@interface ShopHeaderBannerView()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>

@property (nonatomic, strong) NewPagedFlowView *pageFlowView;

@end

@implementation ShopHeaderBannerView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.sourceArr = [NSMutableArray array];
        
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setSourceArr:(NSMutableArray *)sourceArr{
    _sourceArr = sourceArr;
    
    self.pageFlowView.orginPageCount = _sourceArr.count;
    [self.pageFlowView reloadData];
}
#pragma mark - UI
-(void)initUI{
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, 0, BannerWidth, BannerHeight)];
    pageFlowView.backgroundColor = [UIColor clearColor];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.4;
    pageFlowView.minimumPageScale = 0.85;
    pageFlowView.orginPageCount = _sourceArr.count;
    pageFlowView.isOpenAutoScroll = YES;
    pageFlowView.autoTime = 3.0f;
    
    UIScrollView *bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, BannerWidth, BannerHeight)];
    bottomScrollView.backgroundColor = [UIColor clearColor];
    [pageFlowView reloadData];
    [bottomScrollView addSubview:pageFlowView];
    [self addSubview:bottomScrollView];
    [bottomScrollView addSubview:pageFlowView];
    self.pageFlowView = pageFlowView;
}

#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(BannerWidth, BannerHeight);
}
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    if (self.touchBlock) {
        self.touchBlock(subIndex);
    }
}
#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    return _sourceArr.count;
}
- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    
    MainPageHeaderModel *model = self.sourceArr[index];
    
    AdvertCoverView *bannerView = (AdvertCoverView *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[AdvertCoverView alloc] initWithFrame:CGRectMake(0, 0, BannerWidth, BannerHeight)];
    }
    bannerView.imgUrlStr = model.picture;
    return bannerView;
}
- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
}



@end
